package cn.iocoder.yudao.module.member.alliance.service;

import cn.iocoder.yudao.framework.business.audit.enums.AuditStatusEnum;
import cn.iocoder.yudao.framework.business.basic.dao.repository.SerialNoRedisRepository;
import cn.iocoder.yudao.framework.business.order.service.AppOrderBaseService;
import cn.iocoder.yudao.framework.common.exception.Assertions;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.api.member.alliance.vo.CloudAllianceOrderVO;
import cn.iocoder.yudao.module.api.pay.bill.enums.BillTypeEnum;
import cn.iocoder.yudao.module.api.pay.order.PayOrderApi;
import cn.iocoder.yudao.module.api.pay.order.dto.PayOrderCreateDTO;
import cn.iocoder.yudao.module.member.alliance.dal.dataobject.CloudAllianceApplyDO;
import cn.iocoder.yudao.module.member.alliance.dal.dataobject.CloudAllianceOrderDO;
import cn.iocoder.yudao.module.member.alliance.dal.dataobject.CloudAlliancePackageDO;
import cn.iocoder.yudao.module.member.alliance.dal.mysql.CloudAllianceOrderMapper;
import cn.iocoder.yudao.module.member.alliance.dal.mysql.CloudAlliancePackageMapper;
import cn.iocoder.yudao.module.member.alliance.mq.CloudAllianceProducer;
import cn.iocoder.yudao.module.member.alliance.vo.AppCloudAllianceOrderVO;
import cn.iocoder.yudao.module.member.alliance.vo.CloudAllianceOrderSubmitDTO;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.Objects;

import static cn.iocoder.yudao.module.api.member.ConfigConstants.CLOUD_ALLIANCE_ORDER_NO_PREFIX;

/**
 * 云盟商身份订单 Service 实现类
 *
 * @author 山野羡民
 */
@Slf4j
@Service
@Validated
public class AppCloudAllianceOrderServiceImpl extends AppOrderBaseService<CloudAllianceOrderDO, AppCloudAllianceOrderVO, CloudAllianceOrderSubmitDTO> implements AppCloudAllianceOrderService {
    @Resource
    private CloudAllianceOrderMapper cloudAllianceOrderMapper;
    @Resource
    private CloudAlliancePackageMapper cloudAlliancePackageMapper;


    @Resource
    private CloudAllianceApplyService cloudAllianceApplyService;
    @Resource
    private AppCloudAllianceUserService appCloudAllianceUserService;

    @Resource
    private SerialNoRedisRepository serialNoRedisRepository;

    @Resource
    private PayOrderApi payOrderApi;

    @Resource
    private CloudAllianceProducer cloudAllianceProducer;

    @Override
    protected CloudAllianceOrderMapper getInjectedMapper() {
        return cloudAllianceOrderMapper;
    }

    @Nonnull
    @Override
    protected CloudAllianceOrderDO onOrderCreateBefore(@Nonnull CloudAllianceOrderSubmitDTO reqVO, @Nonnull Long userId) {
        CloudAllianceApplyDO applyDO = cloudAllianceApplyService.getDetailByUserId(userId, null);
        Assertions.notNull(applyDO, "云盟商申请资料不存在");
        Assertions.isTrue(Objects.equals(applyDO.getAuditStatus(), AuditStatusEnum.PASS.getValue()), "云盟商申请资料还未审核通过");
        // 校验云盟商缴费套餐存在
        CloudAlliancePackageDO packageDO = cloudAlliancePackageMapper.selectById(reqVO.getPackageId());
        Assertions.notNull(packageDO, "云盟商缴费套餐不存在");
        Assertions.equals(packageDO.getType(), applyDO.getType(), "云盟商缴费套餐无效");
        // 构建订单信息
        CloudAllianceOrderDO order = new CloudAllianceOrderDO();
        order.setUserId(userId);
        order.setPackageId(packageDO.getId());
        order.setType(packageDO.getType());
        order.setDays(packageDO.getDays());
        order.setPrice(packageDO.getPrice());
        order.setSnapshot(JsonUtils.toJsonString(packageDO));
        order.setTradeNo(serialNoRedisRepository.generateNo(CLOUD_ALLIANCE_ORDER_NO_PREFIX));
        order.setNote("购买云盟商身份套餐【%s】".formatted(packageDO.getName()));
        return order;
    }

    @Async
    @Override
    public void createPayOrder(@Nonnull CloudAllianceOrderDO order) {
        PayOrderCreateDTO createDTO = new PayOrderCreateDTO();
        createDTO.setTradeNo(order.getTradeNo());
        createDTO.setSubject(BillTypeEnum.PAY_ALLIANCE.getName());
        createDTO.setBody(order.getNote());
        createDTO.setPrice(order.getPrice());
        String payNo = payOrderApi.createOrder(createDTO);
        // 创建付款单据后再回来更新业务订单的支付单号
        CloudAllianceOrderDO updateObj = new CloudAllianceOrderDO();
        updateObj.setId(order.getId());
        updateObj.setPayNo(payNo);
        getInjectedMapper().updateById(updateObj);
    }

    @Override
    protected void onPaidAfter(CloudAllianceOrderDO order) {
        cloudAllianceApplyService.updateWhenPaid(order.getTradeNo(), order.getUserId(), order.getPackageId());
        appCloudAllianceUserService.updateWhenPaid(order);
        cloudAllianceProducer.sendCloudAllianceOrderPaidMessage(BeanUtils.toBean(order, CloudAllianceOrderVO.class));
    }

}